/*
 * Copyright (C) 2011 Project R'yleh Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.Arkham;

import javax.microedition.khronos.opengles.GL10;
import javax.microedition.khronos.opengles.GL11;

import android.util.Log;




public class Square extends Mesh
{
	private final static String TAG = Square.class.getSimpleName();
	
	
	public Square()
	{
		this(1,1);
		
	}
	
	public Square(float width, float height)
	{
		float halfWidth = width * 0.5f;
		float halfHeight = height * 0.5f;
		
		 float[] Vertices = new float[]
		{
				
				-halfWidth, -halfHeight, 0.0f, //0, bottom left
				 halfWidth, -halfHeight, 0.0f, //1, top left
				-halfWidth,  halfHeight, 0.0f, //2, bottom right
				 halfWidth,  halfHeight, 0.0f, //3, top right
		};
		
		 short[] indices =  new short[] { 0 , 1, 2,1,3,2};
		
		 float[] mTextureCoords = new float[] { 0.0f, 1.0f,
								                1.0f, 1.0f,
									            0.0f, 0.0f,
									            1.0f, 0.0f };
		
		 mRGBA[0]= 1.0f;
		 mRGBA[1]=1.0f;
		 mRGBA[2]=1.0f; 
		 mRGBA[3]= 1.0f;
		 
		 
		 setIndices(indices);
		 setVertices(Vertices);
		 setTexCoords(mTextureCoords);
		 //setColorBuffer(mRGBA);
			modelView = new float[16];
		 
	}
	
	protected void OnDraw(GL10 gl)
	{
	
		//Log.i(this.TAG,"In Square Draw");
	
		gl.glMatrixMode(GL10.GL_MODELVIEW);
		((GL11)gl).glGetFloatv(GL11.GL_MODELVIEW_MATRIX, modelView, 0);
		gl.glColor4f(mRGBA[0], mRGBA[1], mRGBA[2], mRGBA[3]);
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mVertexBuffer);
		
		
		if(mColorBuffer != null)
		{
			gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
			gl.glColorPointer(4, GL10.GL_FLOAT, 0, mColorBuffer);
		}
		
		if (mShouldLoadTexture)
		{
			loadGLTexture(gl);
			
			mShouldLoadTexture = false;			
		}
		
		if(mTexID[0] != -1 && mTextureBuffer != null)
		{
			gl.glEnable(GL10.GL_TEXTURE_2D);
			gl.glBindTexture(GL10.GL_TEXTURE_2D, mTexID[0]);
			
			
			gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
			gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, mTextureBuffer);
			
			
		}
		gl.glMatrixMode(GL10.GL_MODELVIEW);
		gl.glPushMatrix();
		gl.glTranslatef(mTranslate[0], mTranslate[1], mTranslate[2]);
		gl.glRotatef(mRotation[0], 1, 0, 0 );
		gl.glRotatef(mRotation[1], 0, 1, 0 );
		gl.glRotatef(mRotation[2], 0, 0, 1 );
		
		
		
		gl.glDrawElements(GL10.GL_TRIANGLES, mNumIndices,
						  GL10.GL_UNSIGNED_SHORT, mIndexBuffer);
		gl.glPopMatrix();
		gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
		
		
		if(mTexID[0] != -1 && mTextureBuffer != null)
		{
			gl.glBindTexture(GL10.GL_TEXTURE_2D, 0);
			gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
			gl.glDisable(GL10.GL_TEXTURE_2D);
		}
		

	
	

		gl.glFinish();
		
	}
	
	
	
}
